package com.qcxy.gzy.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.qcxy.gzy.activity.R;

/**
 * 带进度的进度条，线程安全的View，可直接在线程中更新进度
 * 
 * @author gzy
 *
 */
public class RoundProgressBar extends View {
    /** 画笔对象的引用 */
    private Paint           mPaint     = null;
    private Paint           mPaintUnit = null;

    /** 圆环的颜色 */
    private int             mRoundColor;

    /** 圆环进度的颜色 */
    private int             mRoundProgressColor;

    /** 中间进度百分比的字符串的颜色 */
    private int             mTextColor;
    /** 中间进度百分比的字符串的颜色 */
    private int             mTextUnitColor;

    /** 中间进度百分比的字符串的字体 */
    private float           mTextSize;

    /** 中间单位的字符串的字体 */
    private float           mTextUnitSize;

    /** 圆环的宽度 */
    private float           mRoundWidth;

    /** 最大进度 */
    private int             mMax;

    /** 当前进度 */
    private int             mProgress;

    /** 是否显示中间的进度 */
    private boolean         mTextIsDisplayable;

    /** 进度的风格，实心或者空心 */
    private int             mStyle;

    private String          mUnit      = "";
    private String          mContent   = "";

    public static final int STROKE     = 0;
    public static final int FILL       = 1;

    public String getUnit() {
        return mUnit;
    }

    public void setUnit(String unit) {
        this.mUnit = unit;
    }

    public String getContent() {
        return mContent;
    }

    public void setContent(String mContent) {
        this.mContent = mContent;
    }

    public RoundProgressBar(Context context) {
        this(context, null);
    }

    public RoundProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        mPaint = new Paint();
        mPaintUnit = new Paint();

        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);

        // 获取自定义属性和默认值
        mRoundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.WHITE);
        mRoundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.RED);
        mTextColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.BLUE);
        mTextUnitColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GRAY);
        mTextSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 30);
        mTextUnitSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 18);
        mRoundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);
        mMax = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);
        mTextIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);
        mStyle = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);
        
        mTypedArray.recycle();
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        /**
         * 画最外层的大圆环
         */
        // 获取圆心的x坐标
        int centre = getWidth() / 2;
        // 圆环的半径
        int radius = (int) (centre - mRoundWidth / 2);
        // 设置圆环的颜色
        mPaint.setColor(mRoundColor);
        // 设置空心
        mPaint.setStyle(Paint.Style.STROKE);
        // 设置圆环的宽度
        mPaint.setStrokeWidth(mRoundWidth);
        // 消除锯齿
        mPaint.setAntiAlias(true);
        // 画出圆环
        canvas.drawCircle(centre, centre, radius, mPaint);

        /**
         * 画进度百分比
         */
        mPaint.setStrokeWidth(0);
        mPaint.setColor(mTextColor);
        mPaint.setTextSize(mTextSize);
        mPaint.setTextAlign(Align.CENTER);

        mPaintUnit.setTextSize(mTextUnitSize);
        mPaintUnit.setColor(mTextUnitColor);
        mPaintUnit.setTextAlign(Align.CENTER);

        if (mTextIsDisplayable && mStyle == STROKE) {
            // 画出值
            canvas.drawText(mContent + "", getMeasuredWidth() / 2 - getWidth() / 8 + 10, getMeasuredHeight() / 2 + getHeight() / 15, mPaint);
            // 画出单位
            canvas.drawText(mUnit, getMeasuredWidth() / 2 - getWidth() / 6 + 20, getMeasuredHeight() / 2 + getHeight() / 8 + 20, mPaintUnit);
        }

        /**
         * 画圆弧 ，画圆环的进度
         */
        // 设置进度是实心还是空心
        // 设置圆环的宽度
        mPaint.setStrokeWidth(mRoundWidth);
        // 设置进度的颜色
        mPaint.setColor(mRoundProgressColor);
        RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); // 用于定义的圆弧的形状和大小的界限

        switch (mStyle) {
            case STROKE: {
                mPaint.setStyle(Paint.Style.STROKE);
                // 根据进度画圆弧
                canvas.drawArc(oval, 0, 360 * mProgress / mMax, false, mPaint);
                break;
            }
            case FILL: {
                mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
                if (mProgress != 0) {
                    // 根据进度画圆弧
                    canvas.drawArc(oval, 0, 360 * mProgress / mMax, true, mPaint);

                }
                break;
            }
        }

    }

    public synchronized int getMax() {
        return mMax;
    }

    /**
     * 设置进度的最大值
     * 
     * @param max
     */
    public synchronized void setMax(int max) {
        if (max < 0) {
            throw new IllegalArgumentException("max not less than 0");
        }
        this.mMax = max;
    }

    /**
     * 获取进度.需要同步
     * 
     * @return
     */
    public synchronized int getProgress() {
        return mProgress;
    }

    /**
     * 设置进度，此为线程安全控件，由于考虑多线的问题，需要同步 刷新界面调用postInvalidate()能在非UI线程刷新
     * 
     * @param progress
     */
    public synchronized void setProgress(int progress) {
        if (progress < 0) {
            throw new IllegalArgumentException("progress not less than 0");
        }
        if (progress > mMax) {
            progress = mMax;
        }
        if (progress <= mMax) {
            this.mProgress = progress;
            postInvalidate();
        }

    }

    public int getCricleColor() {
        return mRoundColor;
    }

    public void setCricleColor(int cricleColor) {
        this.mRoundColor = cricleColor;
    }

    public int getCricleProgressColor() {
        return mRoundProgressColor;
    }

    public void setCricleProgressColor(int cricleProgressColor) {
        this.mRoundProgressColor = cricleProgressColor;
    }

    public int getTextColor() {
        return mTextColor;
    }

    public void setTextColor(int textColor) {
        this.mTextColor = textColor;
    }

    public float getTextSize() {
        return mTextSize;
    }

    public void setTextSize(float textSize) {
        this.mTextSize = textSize;
    }

    public float getRoundWidth() {
        return mRoundWidth;
    }

    public void setRoundWidth(float roundWidth) {
        this.mRoundWidth = roundWidth;
    }

}
